#验证识别测试
import tesserocr
from PIL import Image
import requests
import time
from http import cookiejar as cookielib

session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename='cookie.txt')
try:
    session.cookies.load(ignore_discard=True)
except:
    print("cookie未加载")
def getCaptcha():
    '''爬取指定url页面信息'''
    try:
        #定义请求头信息

        headers = {
            'User-Agent':'User-Agent:Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1'
        }
        # 执行爬取
        rom = str(int(time.time() * 1000))
        print(rom)
        captcha_url='http://ei.ssp.eastdaymedia.com.cn/media-console/user/authImage.api?userName=17611640600&rom={}'.format(rom)
        res = session.get(captcha_url,headers=headers)
        print(res.status_code)
        with open('captcha.jpg','wb') as f:
            f.write(res.content)
            f.close()
        im = Image.open('captcha.jpg')
        im.show()
        im.close()
        captcha = input("请输入验证码:")
        return captcha
    except Exception as e:
        print(e)
        return None

def login():
    url = 'http://ei.ssp.eastdaymedia.com.cn/media-console/user/login.api'
    headers = {
        'User-Agent': 'User-Agent:Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1'
    }
    data = {
    'userName': '17611640600',
    'passWord': 'qwer123',
    'imgCode': getCaptcha(),
    'accountType': 0
    }
    content = session.post(url,data,headers=headers)
    session.cookies.save()
    print(content.content)
    return


if __name__=="__main__":
    login()